﻿using Hrm.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper.QueryableExtensions;

namespace Hrm.Services
{
    public class RoleService : BaseService<Role>, IRoleService
    {
        private readonly IRolePermissionDbRepository _roleRepository;
        private readonly IResourcePermissionDbRepository _resourceRepository;
        private readonly IResourceAndPermissionPermissionDbRepository _resourceAndPermissionRepository;

        public RoleService(IRolePermissionDbRepository roleRepository, IResourcePermissionDbRepository resourceRepository, IResourceAndPermissionPermissionDbRepository resourceAndPermissionRepository)
            : base(roleRepository)
        {
            this._roleRepository = roleRepository;
            this._resourceRepository = resourceRepository;
            this._resourceAndPermissionRepository = resourceAndPermissionRepository;
        }

        public List<string> GetRoleCodesByUserCode(string userCode)
        {
            var query = from p in this.GetAllAbledAndOrderByCreatetime()
                        where p.Users.Any(q => q.Code == userCode)
                        select p.Code;
            return query.ToList();
        }

        public List<string> GetRoleCodesByResource(string areaName, string controllerName, string actionName)
        {
            string tempAreaName = string.IsNullOrEmpty(areaName) ? "" : "/" + areaName;
            string resourceUrl = string.Format("{0}/{1}/{2}", tempAreaName, controllerName, actionName);
            var query = this._roleRepository.GetList(p => p.ResourceAndPermissions == null ? false : p.ResourceAndPermissions.Any(x => x.Resource.Url == resourceUrl)).Select(p => p.Code);
            return query.ToList();
        }

        public PageResponse<RoleDto> GetPageList(PageRequest<RoleDto> request)
        {
            var query = this.GetAllAbledAndOrderByCreatetime();
            if (!string.IsNullOrEmpty(request.filter.Name))
            {
                query = query.Where(p => p.Name.Contains(request.filter.Name));
            }

            if (!string.IsNullOrEmpty(request.filter.Code))
            {
                query = query.Where(p => p.Code == request.filter.Code);
            }
            var data = query.Project().To<RoleDto>();

            PageResponse<RoleDto> result = new PageResponse<RoleDto>(data, request.draw, request.start, request.length);
            return result;
        }


        public List<ResourcePermissionTreeNodeDto> ResourcePermissionData(int appId, int roleId)
        {
            var ResourcePermissionTree = new List<ResourcePermissionTreeNodeDto>();
            var role=this._roleRepository.GetById(roleId);
            var permissioin = this._resourceAndPermissionRepository.GetList(p => p.Resource.Application.Id == appId)
                .Select(p => new ResourcePermissionTreeNodeDto()
                              {
                                  Id = p.Id.ToString(),
                                  PId = p.Resource.Code,
                                  Check = role.ResourceAndPermissions!=null?role.ResourceAndPermissions.Any(y => y.Id == p.Id):false,
                                  open = false,
                                  iconSkin = "Permission",
                                  Name = p.Permission.Name,
                                  Type = "Permission"
                              }).ToList();
            var Resource = this._resourceRepository.GetList(p => p.Application.Id == appId)
                .Select(p => new ResourcePermissionTreeNodeDto()
                        {
                            Id = p.Code,
                            PId = p.ParentCode,
                            Check = false,
                            open = permissioin.Any(x => x.PId == p.Code && x.Check == true),
                            iconSkin = "Resource",
                            Name = p.Name,
                            Type = "Resource"
                        }).ToList();
            ResourcePermissionTree.AddRange(Resource);
            ResourcePermissionTree.AddRange(permissioin);
            return ResourcePermissionTree;
        }
    }
}
